package com.boarsoft.boar.config.app.action;

import com.boarsoft.boar.BaseConfig;
import com.boarsoft.boar.config.service.ConfigFileService;
import com.boarsoft.boar.config.service.ConfigVarService;
import com.boarsoft.common.util.ZipUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.boarsoft.bean.ReplyInfo;
import com.boarsoft.boar.app.action.AppBaseAction;
import com.boarsoft.boar.config.entity.AppConfigInfo;
import com.boarsoft.boar.entity.AppInfo;
import com.boarsoft.common.Authorized;
import com.boarsoft.common.dao.PagedResult;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

@RestController
@RequestMapping("/app")
public class AppConfigAction extends AppBaseAction {

	@Autowired
	private ConfigFileService configFileService;

	@Autowired
	private ConfigVarService configVarService;

	/**
	 * 
	 * @param parentId
	 * @param o
	 * @return
	 */
	@RequestMapping("/save.do")
	//@Authorized(code = "app.save")
	public ReplyInfo<Object> save(String parentId, AppConfigInfo o) {
		return super.save(parentId, o);
	}

	/**
	 * 分页查询所有的APP
	 * 
	 * @return
	 */
	@JsonIgnoreProperties({ "libs", "insts", "apps", "files" })
	@RequestMapping("/list.do")
	public ReplyInfo<Object> list(String id, String projId, String code, String name, String key, String orderBy, int pageNo, int pageSize) {
		PagedResult<AppInfo> pr = appBiz.list(id, projId, code, name, key, orderBy, pageNo, pageSize);
		return new ReplyInfo<>(true, pr);
	}

	@RequestMapping("/clone.do")
	public ReplyInfo<Object> clone(String appId, String fromEnv, String toEnv) {
		//1.调用参数服务进行参数克隆
		configVarService.clone(appId,fromEnv,toEnv);
		//2.调用参数文件服务进行克隆
		configFileService.clone(appId,fromEnv,toEnv);
		return new ReplyInfo<>(true);
	}

	@RequestMapping("/export.do")
	public ReplyInfo<Object> configExport(String appId, String fromEnv) throws FileNotFoundException {
		//1.设置导出根目录
		StringBuilder sb=new StringBuilder(BaseConfig.PATH_PRIVATE);
		String exportRoot=sb.append("/").append("configexport")
				.append("/").append(System.currentTimeMillis()).toString();
		//2.导出配置变量
		configVarService.exportConfig(appId,fromEnv,exportRoot);
		//3.导出配置文件与表
		configFileService.exportConfig(appId,fromEnv,exportRoot);
		//4.将根目录打成zip包
		ZipUtil.zip(sb.append("/").append("configexport")
				.append("/").append(System.currentTimeMillis())
				.append(".zip").toString(),exportRoot);
		return new ReplyInfo<>(true,exportRoot);
	}

	@RequestMapping("/import.do")
	public ReplyInfo<Object> configImport(String zipPath) throws IOException {
		//1.解压zip包
		String importRoot=new StringBuilder(BaseConfig.PATH_PRIVATE)
				.append("/")
				.append("configimport")
				.append("/").toString();
		ZipUtil.unzip(new File(zipPath),importRoot);
		//2.找到配置变量目录,导入配置变量
		configVarService.importConfig(importRoot);
		//3.找到配置文件目录，导入配置文件
		configFileService.importConfig(importRoot);
		return new ReplyInfo<>(true);
	}






}